﻿/// <reference path="_namespace.js" />
/// <reference path="../BaseObject.js" />
/// <reference path="../IComparable.js"/>

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.effects.NotifyFlash
/*****************************************************************************/


ajax.lang.addNamespace("ajax.drawing");
$imports("ajax.IComparable");

//-----------------------------------------------------------------------------
ajax.drawing.Colour = function(r, g, b, a)
{
    
    ajax.drawing.Colour.superConstructor.call(this);
    
    this.r = r;
    this.g = g;
    this.b = b;             
    this.a = 1;
    if (ajax.isDefined(a))
        this.a = Math.max(Math.min(a, 1)); // Constrain to 0 to 1.
}

//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.drawing.Colour", "ajax.BaseObject");
ajax.lang.supports("ajax.drawing.Colour", "ajax.IComparable");
//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.r     = 0;
ajax.drawing.Colour.prototype.g     = 0;
ajax.drawing.Colour.prototype.b     = 0;
ajax.drawing.Colour.prototype.a     = 0;

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.compareTo = function(colourB)
{///<param name="colourB" type="ajax.drawing.Colour">The colour to compare.</param>
 ///<returns type="ajax.CompareResult"/>
    if (colourB.a == this.a && colourB.r == this.r && colourB.g == this.g && colourB.b == this.b)
        return ajax.CompareResult.Equals;        
    
    var thisTotalColor = (this.a + this.r + this.g + this.b) * this.a;
    var colBTotalColor = (colourB.a + colourB.r + colourB.g + colourB.b) * colourB.a;
    
    if (thisTotalColor < colBTotalColor)
        return ajax.CompareResult.LessThan;
    
    return ajax.CompareResult.GreaterThan;        
}


//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.setValues = function(r, g, b, a)
{
    if (r || g || b || a)
    {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
    // Constrain to 0 to 255 values
    this.r = Math.max(Math.min(this.r, 255), 0);
    this.g = Math.max(Math.min(this.g, 255), 0);
    this.b = Math.max(Math.min(this.b, 255), 0);
    this.a = Math.max(Math.min(this.a, 255), 0);
           
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.toHex = function(prefix)
{
    return ajax.drawing.Colour.rgbToHex(this.r, this.g, this.b, prefix);
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.getInverse = function()
{
    return ajax.drawing.Colour.getInverse(this);
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.invert = function()
{
    var inv = this.getInverse();
    this.setValues(inv.r, inv.g, inv.b);
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.calculateFogging = function(floatIntensity, floatDistance)
{
    return 1 - floatDistance * floatIntensity;
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.divideScalar = function(scalarValue)
{
    this.r /= scalarValue;
    this.g /= scalarValue;
    this.b /= scalarValue;       
}
//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.multiplyScalar = function(scalarValue)
{
    this.r *= scalarValue;
    this.g *= scalarValue;
    this.b *= scalarValue;    
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.prototype.toString = function()
{
    return "ajax.drawing.Colour";
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.cast = function(obj)
{///<returns type="ajax.drawing.Colour"/>
    return obj;            
}



// Static methods
//-----------------------------------------------------------------------------
ajax.drawing.Colour.byteToHex = function(n)
{///<returns type="string"/>
    var nybHexString = "0123456789ABCDEF";
    return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.rgbToHex = function(r,g,b, prefix)
{///<returns type="string"/>
    var obj = ajax.drawing.Colour;    
    prefix = typeof(prefix) == "string" ? prefix : "#";
    return prefix + obj.byteToHex(r) + obj.byteToHex(g) + obj.byteToHex(b);
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.toRgbaString = function(colour)
{///<returns type="string">Returns a string "rgba(r,g,b,a)" for use with canvas</returns>
    return ["rgba(", colour.r, ",", colour.g, ",", colour.b, ",", colour.a, ")"].join("");
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.fromHex = function(hex_string, default_)
{///<returns type="ajax.drawing.Colour"/>
    default_ = typeof(default_) != "undefined" ? default_ : null;
    

    if (hex_string.substr(0, 1) == '#')
        hex_string = hex_string.substr(1);


    var r;
    var g;
    var b;
    if (hex_string.length == 3)
    {
        r = hex_string.substr(0, 1);
        r += r;
        g = hex_string.substr(1, 1);
        g += g;
        b = hex_string.substr(2, 1);
        b += b;
    }
    else if (hex_string.length == 6)
    {
        r = hex_string.substr(0, 2);
        g = hex_string.substr(2, 2);
        b = hex_string.substr(4, 2);
    }
    else
        return default_;

    r = parseInt(r, 16);
    g = parseInt(g, 16);
    b = parseInt(b, 16);
    if (isNaN(r) || isNaN(g) || isNaN(b))
        return default_;

    //return {r: r / 255, g: g / 255, b: b / 255};
    return new ajax.drawing.Colour(r, g, b);         
}


//-----------------------------------------------------------------------------
ajax.drawing.Colour.getInverse = function(c)
{///<returns type="ajax.drawing.Colour"/>
    var ret = new ajax.drawing.Colour(255-c.r, 255-c.g, 255-c.b);
    return ret;
}


//-----------------------------------------------------------------------------
ajax.drawing.Colour.getTypes = function()
{
    return new ajax.drawing.Colour.Types();
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.Types = function(){}
ajax.drawing.Colour.Types.prototype = 
{
    Rgb: {center: 255/2, width: 255/2},
    Pastel: {center: 200, width: 55},
    LightPastel: {center: 230, width: 25}
}
//-----------------------------------------------------------------------------
ajax.drawing.Colour.Types.cast = function(obj)
{///<returns type="ajax.drawing.Colour.Types"/>
    return obj;
}

//-----------------------------------------------------------------------------
ajax.drawing.Colour.cornflowerBlue = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(102, 149, 237);
}
ajax.drawing.Colour.black = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(0, 0, 0);
}
ajax.drawing.Colour.white = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(255, 255, 255);
}
ajax.drawing.Colour.red   = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(255, 0, 0);
}
ajax.drawing.Colour.green = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(0, 255, 0);
}
ajax.drawing.Colour.blue  = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(0, 0, 255);
}
ajax.drawing.Colour.darkGray = function()
{///<returns type="ajax.drawing.Colour"/>
    return new ajax.drawing.Colour(20, 20, 20);
}